﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.Common;
namespace BestBuy.PageUser
{
    public partial class ReadMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                loadMessage();   
            }             
        }
        private static int idmess;
        private void loadMessage()
        {            
            idmess = int.Parse(Request.Params["IdMessage"].ToString());
            BMessage mess = CMessage.getMessage(idmess);
            if (mess.Status == 3)
            {
                deleteMess.Text = "Delete forever";
                deleteMess1.Text = "Delete forever";
            }
            ltSender.Text = CMessage.getNameSender(mess.IDTKSender);
            ltTitle.Text = CMessage.getTitle(mess.IDTKReceiver, mess.Source, mess.IDContact);
            ltDate.Text = mess.Date;
            ltContent.Text = mess.Subject;
            if (mess.Status == 1)
            {
                CMessage.updateStatusMessage(idmess);
            }
            unread.Text = CMessage.countMessage(int.Parse(Session["IDDN"].ToString())).ToString();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            BMessage mess = CMessage.getMessage(idmess);
            int idcongty = cManageSellOffer.getIdCompany(mess.IDTKSender);
            ltName.Text = CMessage.getNameSender(mess.IDTKSender);
            ltCompany.Text = cManageSellOffer.getNameCompany(idcongty).ToString();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            BMessage rep = new BMessage();
            BMessage mess = CMessage.getMessage(idmess);
            rep.IDTKReceiver = mess.IDTKSender;
            rep.IDTKSender = mess.IDTKReceiver;
            rep.Subject = tbReply.Text;
            rep.Date = DateTime.Now.ToShortDateString();
            rep.Source = mess.Source;
            rep.Status = 1;
            rep.IDContact = mess.IDContact;
            CMessage.addMessage(rep);
            Response.Redirect("Message.aspx");
        }

        protected void deleteMess_Click(object sender, EventArgs e)
        {
            
            BMessage mess = CMessage.getMessage(idmess);
            if (mess.Status != 3)
            {
                int status = (int)MessageStatus.Erase;
                CMessage.eraseMessage(idmess, status);
                Response.Redirect("Message.aspx");
            }
            else
            {
                int status = (int)MessageStatus.Deleted ;
                CMessage.eraseMessage(idmess, status);
                Response.Redirect("DeleteMessage.aspx");
            }
            
        }
    }
}